<?php

App::uses('AppController', 'Controller');

/**
 * Roles Controller
 *
 * @property Role $Role
 */
class RolesController extends AppController {
//    public $actAs = array('Acl' => array('type' => 'requester'));
//    
//    public function parentNode(){
//        return null;
//    }

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->Role->recursive = 0;
        $this->set('roles', $this->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->Role->exists($id)) {
            throw new NotFoundException('Invalid role', 'default', array('class' => 'alert alert-danger'));
        }
        $options = array('conditions' => array('Role.' . $this->Role->primaryKey => $id));
        $this->set('role', $this->Role->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->Role->create();
            if ($this->Role->save($this->request->data)) {
                $this->Session->setFlash('The new role has been saved', 'default', array('class' => 'alert alert-info'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('The new role could not be saved. Please, try again.', 'default', array('class' => 'alert alert-danger'));
            }
        }
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        if (!$this->Role->exists($id)) {
            throw new NotFoundException('Invalid role', 'default', array('class' => 'alert alert-danger'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Role->save($this->request->data)) {
                $this->Session->setFlash('The role has been successfully edited', 'default', array('class' => 'alert alert-info'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('The role could not be edited. Please, try again.', 'default', array('class' => 'alert alert-danger'));
            }
        } else {
            $options = array('conditions' => array('Role.' . $this->Role->primaryKey => $id));
            $this->request->data = $this->Role->find('first', $options);
        }
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->Role->id = $id;
        if (!$this->Role->exists()) {
            throw new NotFoundException('Invalid role', 'default', array('class' => 'alert alert-danger'));
        }
        $this->request->onlyAllow('post', 'delete');

        $this->loadModel('User');
        $user = $this->User->find('all', array('conditions' => array('User.role_id' => $id)));

        if (empty($user)) {

            if ($this->Role->delete()) {
                $this->Session->setFlash('Role deleted', 'default', array('class' => 'alert alert-info'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Role was not deleted', 'default', array('class' => 'alert alert-danger'));
                $this->redirect(array('action' => 'index'));
            }
        } else {
            $this->Session->setFlash('Role was not deleted, please change users roles before deleting', 'default', array('class' => 'alert alert-danger'));
            $this->redirect(array('action' => 'index'));
        }
    }

    /**
     * admin_index method
     *
     * @return void
     */
    public function admin_index() {
        $this->Role->recursive = 0;
        $this->set('roles', $this->paginate());
    }

    /**
     * admin_view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_view($id = null) {
        if (!$this->Role->exists($id)) {
            throw new NotFoundException('Invalid role', 'default', array('class' => 'alert alert-danger'));
        }
        $options = array('conditions' => array('Role.' . $this->Role->primaryKey => $id));
        $this->set('role', $this->Role->find('first', $options));
    }

    /**
     * admin_add method
     *
     * @return void
     */
    public function admin_add() {
        if ($this->request->is('post')) {
            $this->Role->create();
            if ($this->Role->save($this->request->data)) {
                $this->Session->setFlash('The role has been saved', 'default', array('class' => 'alert alert-danger'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('The role could not be saved. Please, try again.', 'default', array('class' => 'alert alert-danger'));
            }
        }
    }

    /**
     * admin_edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_edit($id = null) {
        if (!$this->Role->exists($id)) {
            throw new NotFoundException('Invalid role', 'default', array('class' => 'alert alert-danger'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Role->save($this->request->data)) {
                $this->Session->setFlash('The role has been successfully edited', 'default', array('class' => 'alert alert-info'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('The role could not be edited. Please, try again.', 'default', array('class' => 'alert alert-danger'));
            }
        } else {
            $options = array('conditions' => array('Role.' . $this->Role->primaryKey => $id));
            $this->request->data = $this->Role->find('first', $options);
        }
    }

    /**
     * admin_delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_delete($id = null) {
        $this->Role->id = $id;
        if (!$this->Role->exists()) {
            throw new NotFoundException('Invalid role', 'default', array('class' => 'alert alert-danger'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Role->delete()) {
            $this->Session->setFlash('Role deleted', 'default', array('class' => 'alert alert-info'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash('Role was not deleted', 'default', array('class' => 'alert alert-danger'));
        $this->redirect(array('action' => 'index'));
    }

//    public function beforeFilter() {
//        parent::beforeFilter();
//
//        // For CakePHP 2.1 and up
//        $this->Auth->allow();
//    }
}
